package com.sali.DP;

/**
 * @author sali
 * @date 2025/5/3 21:27
 */
public class LeetCode_309 {

    public int maxProfit(int[] prices) {
        int[][] dp = new int[prices.length + 1][2];

        dp[0][0] = 0;
        dp[0][1] = -prices[0];
        dp[1][0] = Math.max(dp[0][0], dp[0][1] + prices[0]);
        dp[1][1] = Math.max(dp[0][1], dp[0][0] - prices[0]);

        int maxRes = 0;
        for (int i = 2; i < prices.length + 1; i++) {
            dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i - 1]);
            dp[i][1] = Math.max(dp[i - 1][1], dp[i - 2][0] - prices[i - 1]);
            maxRes = Math.max(maxRes, dp[i][0]);
        }

        return maxRes;
    }

}
